layui.use(['upload','element','table','form','laytpl'],function(){
		 var $ = layui.jquery
  		,upload = layui.upload
			,table = layui.table
			,layer = layui.layer
			,element =layui.element
			,form = layui.form
			,laytpl = layui.laytpl
			 //监听提交	
			table.on('tool(studentList)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
				  var data = obj.data; //获得当前行数据
				  var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
				  var tr = obj.tr; //获得当前行 tr 的DOM对象
				 
				  if(layEvent === 'detail'){ //查看
				    //do somehing
				    layer.open({
				    	type: 2,
				    	title: obj.data.name,
				    	shadeClose: false,
				    	shade: true,
				    	maxmin: false, //开启最大化最小化按钮
				    	area: ['800px', '640px'],
				    	content: "editStudentPaper.html?studentNo=" + data.studentNo,
				    	cancel: function(index, layero) {
				    		studentList.reload()
				    		layer.close(index)
				    	}
				    });
				  } else if(layEvent === 'del'){ //删除
				    layer.confirm('真的删除行么', function(index){
				      obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
//				      console.log(data.id)
				      layer.close(index);
//				      向服务端发送删除指令
				      $.ajax({
				      	url:"http://localhost:8888/student/deleteById?id="+data.id,
				      	type:"get",
				      	success:function(data){
				      		layer.msg("删除成功")
				      		location.reload()
				      	}
				      })
				    });
				  } else if(layEvent === 'edit'){ //编辑
				    //do something
				    //同步更新缓存对应的值
			   	 	layer.open({
					      type: 2,
					      title: obj.data.name,
					      shadeClose: false,
					      shade: true,
					      maxmin: false, //开启最大化最小化按钮
					      area: ['800px', '640px'],
					      content: "editStudent.html?id="+data.id,
					      cancel: function(index, layero){ 
						  	studentList.reload()
						    layer.close(index)							
						}    
			    	});
				   
				  }
			});
      
			form.on('submit(submitStudent)', function(data) {
				$.ajax({
					type: 'post',
					url: 'http://localhost:8888/student/insertStudent',
					data: data.field,
					success: function(data) {
						layer.msg("添加成功")
						table.reload('studentList', {
							url: 'http://localhost:8888/student/getAllStudent',
							where: {} //设定异步数据接口的额外参数
							//,height: 300
						});
					},
					error: function() {
						layer.msg("添加失败，请不要重复添加相同学号的学生")
						table().render()
					}
				});
//				console.log(data.field)
				return false;
			});
			
			
			var studentList = table.render({
					elem:'#studentList',
					url:"http://localhost:8888/student/getAllStudent",
					cols:[[
					{field:'id',title:'id'},
						 {field:'studentNo',  title: '学号'}
				      ,{field:'name',  title: '名字', sort: true}
				      ,{field:'gradeInfo',  title: '年级', sort: true}
				      ,{field:'classInfo',  title: '班级', sort: true}
				      ,{field:'major',  title: '专业', sort: true}
				      ,{field:'sex',  title: '性别', templet:function(d){
				      	return d.sex == 0?'女':'男'
				      },sort: true}
				      ,{fixed:'right',width:180,align:'center',title:"操作",toolbar:'#bar'}
					]]
			});
			
			var newStudentList = table.render({
				elem:'#newStudentList',
				url:"http://localhost:8888/student/getUploadExcel",
				cols:[[
					  {field:'id', width:80, title: 'ID', sort: true}
			      ,{field:'studentNo',  title: '学号'}
			      ,{field:'name',  title: '名字', sort: true}
			      ,{field:'gradeInfo',  title: '年级', sort: true}
			      ,{field:'classInfo',  title: '班级', sort: true}
			      ,{field:'major',  title: '专业', sort: true}
			      ,{field:'sex',  title: '性别',templet:function(d){
			      	return d.sex == 0?'女':'男'
			      }, sort: true}
//				      ,{fixed:'right',align:'center',toolbar:'#bar'}
				]]
			});
			
		upload.render({
		    	elem: '#uploadExcel'
			    ,url: 'http://localhost:8888/student/uploadExcel/'
			    ,accept: 'file' //普通文件
			    ,done: function(res, index, upload){
	//	      		var jsonS = JSON.stringify(res.data)
					
		      		newStudentList.reload();
		    },error:function(){
		    		layer.msg("上传失败，检查Excel格式")
		    }
  		});
  		$("#submitStudents").click(function() {
  			$.ajax({
  				url: "http://localhost:8888/student/insertStudents",
  				success: function(data) {
  					layer.msg("上传成功！")
  					studentList.reload()
  				},
  				error: function() {
  					layer.msg("上传失败！")
  				}
  			})
  		})
	})